package utils

import (
	"encoding/json"
	"fmt"
	"strconv"
)

func FloattoInt(f float64) int {
	i, _ := strconv.Atoi(fmt.Sprintf("%1.0f", f))
	return i
}

func StringToInt(s string) int {
	aa, _ := strconv.ParseFloat(s, 32)
	i := FloattoInt(aa)
	return i
}

func StringToBool(s string) bool {
	i, _ := strconv.ParseBool(s)
	return i
}

func StringToFloat(s string) float64 {
	aa, _ := strconv.ParseFloat(s, 64)
	return aa
}

func Abs(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}

func ConvertJsonToMap(obj interface{}) (sp []map[string]string) {
	str, err := json.Marshal(obj)
	if err != nil {
		return nil
	}

	err1 := json.Unmarshal(str, &sp)
	if err1 != nil {
		return nil
	}
	return sp
}

func ConvertMapToJson(sp map[string]string) (obj interface{}) {
	mJson, _ := json.Marshal(sp)
	if err := json.Unmarshal([]byte(mJson), &obj); err != nil {
		return nil
	}
	return
}
